﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Quartz;

namespace TSSControlLibrary
{
    public partial class CronDisplayControl : UserControl
    {
        public delegate void Vaction(string cron);
        public Vaction OpCronExpression;
        public CronDisplayControl()
        {
            InitializeComponent();
            this.button_bc.Enabled = false;
        }
        /// <summary>
        /// 处理表达式字符串
        /// </summary>
        /// <param name="dataCron"></param>
        /// <param name="c"></param>
        public void DealExpression(CronDataNodel dataCron, Control c)
        {
            if (c is CronSecondControl)
            {
                this.textBox_second.Text = dataCron.secondExpression;           
            }
            else if (c is CronMinuteControl)
            {
                this.textBox_minute.Text = dataCron.minuteExpression;               
            }
            else if (c is CronHourControl)
            {
                this.textBox_hour.Text = dataCron.hourExpression;
            }
            else if (c is CronDayControl)
            {
                this.textBox_day.Text = dataCron.dayExpression;
            }
            else if (c is CronMonthControl)
            {
                this.textBox_month.Text = dataCron.monthExpression;
            }
            else if (c is CronWeekControl)
            {
                this.textBox_week.Text = dataCron.weekExpression;
            }
            else if (c is CronYearControl)
            {
                this.textBox_year.Text = dataCron.yearExpresiion;
            }
            ProductCronExpression(dataCron);
        }
        /// <summary>
        /// 生成完整的cron表达式
        /// </summary>
        /// <param name="dataCron"></param>
        private void ProductCronExpression(CronDataNodel dataCron)
        {
            this.textBox_CronReExp.Text = dataCron.secondExpression + 
                dataCron.minuteExpression + dataCron.hourExpression +
                dataCron.dayExpression + dataCron.monthExpression +
                dataCron.weekExpression + dataCron.yearExpresiion;

            try
            {

                DateTimeOffset? t = null;
                DateTimeOffset utcNow = SystemTime.UtcNow();
                CronExpression expression = new CronExpression(this.textBox_CronReExp.Text);
                t = expression.GetNextValidTimeAfter(utcNow);
                DateTime t1 = TimeZone.CurrentTimeZone.ToLocalTime(Convert.ToDateTime(t.ToString()));
                t = expression.GetNextValidTimeAfter((DateTimeOffset)t);
                DateTime t2 = TimeZone.CurrentTimeZone.ToLocalTime(Convert.ToDateTime(t.ToString()));
                t = expression.GetNextValidTimeAfter((DateTimeOffset)t);
                DateTime t3 = TimeZone.CurrentTimeZone.ToLocalTime(Convert.ToDateTime(t.ToString()));
                if (t1.ToString() != null)
                {
                    this.lasttime1.Text = t1.ToString();
                    this.lasttime2.Text = t2.ToString();
                    this.lasttime3.Text = t3.ToString();
                    this.button_bc.Enabled = true;
                }
                else
                {
                    this.lasttime1.Text = "Cron表达式无效";
                    this.lasttime2.Text = "";
                    this.lasttime3.Text = "";
                    this.button_bc.Enabled = false;
                }
            }
            catch(Exception e)
            {
                this.lasttime1.Text = "Cron表达式无效";
                this.lasttime2.Text = "";
                this.lasttime3.Text = "";
                this.button_bc.Enabled = false;
            }
          
        }
        /// <summary>
        /// 保存按钮出发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_bc_Click(object sender, EventArgs e)
        {
            //保存配置好的有效cron表达式
            OpCronExpression(this.textBox_CronReExp.Text);
        }
       
    }
}
